import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { changeTab } from '@/axios/getData'
import { useRouter } from 'vue-router'
const $router = useRouter()

export const useCounterStore = defineStore('counter', {
  state: () => {
    // 定义状态
    return {
      // 购物车
      car: [],
      // 优惠券
      coupon: [],
<<<<<<< HEAD
      num:0
=======
      num: 0,
      //子复选框状态
      check: false,
      flag: '',
      //支付数据
      pay: [],
>>>>>>> 4f74775fa7ff1ba00b67e754027806fe8ea8b13b
    }
  },
  actions: {
    // 获取分类页左侧的数据
    getListLeft(data: any) {
      data.id = data.id * 1
      data.num = data.num * 1
      if (this.car.length > 0) {
        let arr = this.car.filter(item => {
          return item.id == data.id
        })
        if (arr.length > 0) {
          arr[0].num += 1
        } else {
          this.car.push(data)
        }
      } else {
        this.car.push(data)
      }
    },
    //获取加入购物车的数据
    getListId(id: number) {
      let arr = this.car.find(item => item.id == id)
      if (arr) {
        this.car.num++
      }
    },
<<<<<<< HEAD

    addCoupon(data: any) {
      this.coupon.push(data)
      console.log(this.coupon);
    },

=======
>>>>>>> 4f74775fa7ff1ba00b67e754027806fe8ea8b13b
    //获取合计
    getHj() {
      for (let i = 0; i < this.car.length; i++) {
        if (this.car[i].flag) {
          this.num += this.car[i].num * this.car[i].price
        }
      }
<<<<<<< HEAD
    }
=======
    },
    getJiesuan(){
      let arr = this.car.filter(item=>item.flag)
        this.pay = arr
        $router.push('/order')
    },
    changeFlag(id){
      console.log(this.car);
      
    },
>>>>>>> 4f74775fa7ff1ba00b67e754027806fe8ea8b13b
  }
})
